"""==========================================================================
@Uses: This script is created to calculate accuracy of doc retrieval in python
============================================================================"""
import MySQLdb
import csv


User = 'Sangeeta'
Pass = 'sangeeta@123456'

"""
User = 'root'
Pass = '123'
"""

db = MySQLdb.connect(host="localhost", user=User, passwd=Pass,db="vsm_python")

"""top_count is not required but will help in getting the specified table"""
top_count = 10 
No_of_topics =0
""""variable that store for how many docs we have to check our accruracy, top 10, top20, top30, top40, top50 """
top_doc = 10 +1 # Top doc is the document itself,so we have to always give top_doc greater than one
#Number of 
max_dup_doc = 25000
count = 0#No of docs for whihc relevant document is found in the top_doc
"""----------------------------------------------"""


score_table = "vsm_doc_score_python_"+(str)(No_of_topics)+"_"+(str)(top_count)
selectstr = "select distinct  duplicate  from dupdetails_final  limit 0,%(max_dup_doc)d" %{'max_dup_doc':max_dup_doc}
print "select str",selectstr
#No_of_duplicat_docs=0

cursor = db.cursor()
cursor.execute(selectstr)

duplicateIssues = cursor.fetchall()
No_of_duplicate_docs = len(duplicateIssues)

cursor2 = db.cursor()

progress=0
for element in duplicateIssues:
    queryid = element[0]
    #queryid = 100120
    relevant = []
    #print queryid
    str2 = "select mainIssue from dupdetails_final where duplicate = \'%(dup)s\'" %{'dup':queryid}
    #print "str2",str2
    cursor2.execute(str2)
    mainIssue = cursor2.fetchone()[0]
    #print "main", mainIssue
   # mainIssue = issues[0][0]
    relevant.append(mainIssue) 
    str3= "select duplicate from dupdetails_final where mainIssue =\'%(main)s\'"  %{'main':mainIssue}
    cursor2.execute(str3)
    issues = cursor2.fetchall()
    for val in issues:
        #print "val",val
        dupid = val[0]
        if dupid!=queryid:
            relevant.append(dupid)
    #print "dup", queryid, "relevant ", relevant        
    
    str4="select answerdoc from %(score_table)s where querydoc = \'%(dupqueryid)s\' order by score desc limit 0, %(top)d"  %{'score_table':score_table, 'dupqueryid':queryid, 'top':top_doc}       
    #print str4 
    cursor2.execute(str4)
    top_score_doc = cursor2.fetchall()  
    flag =0;  
    for doc in top_score_doc:
        docid = doc[0]
        for relid in relevant:
            if docid==relid:
                flag=1
    if flag!=0:
        count= count+1
    progress=progress+1
    print "progress=",progress    
        
#Calculate Accuracy
accuracy  = ((float)(count)*100)/No_of_duplicate_docs   
print "Total doc=",No_of_duplicate_docs,"  Count=",count  
print accuracy

str5 = "insert into accuracy_table values(%(no_of_topics)d, %(top_docs)d, %(accuracy)f, %(total_docs)d)" %{'no_of_topics':No_of_topics, 'top_docs':top_doc, 'accuracy':accuracy, 'total_docs':No_of_duplicate_docs}
print "str5=",str5
cursor2.execute(str5)
db.commit()
   
     
   

        
    